package com.icoolkj.learn.juc.base.communication;

import com.icoolkj.learn.juc.base.utils.SleepTools;
import lombok.extern.slf4j.Slf4j;

/**
 * @author icoolkj
 * @version 1.0
 * @description
 * @createDate 2025/03/05 12:10
 */
@Slf4j
public class WaitDemo {

    public static void main(String[] args) {
        Object lock = new Object();

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    synchronized (lock) {
                        log.info("Wait start");
                        lock.wait();  // wait 会释放锁
                    }
                    log.info("Wait end");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "t1");

        t1.start();

        // 保证 t1先启动， wait() 先执行
        SleepTools.second(1);

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock) {
                    log.info("notify start");
                    lock.notifyAll();
                    log.info("notify end");
                }
            }
        });

        t2.start();


    }

}
